#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>

using namespace std;

struct personInfo
{
	string name;
	string inTime;
	string outTime;
};

//void fillData(vector<vector<personInfo> > &inoutData)
//{
//	personInfo tmp1 = {"ME3021112225321", "00:00:00", "23:59:59"};
//	personInfo tmp2 = {"EE301218", "08:05:35", "20:56:35"};
//	personInfo tmp3 = {"MA301134", "12:35:45", "21:40:42"};
//	personInfo tmp4 = {"CS301111", "15:30:28", "17:00:10"};
//	personInfo tmp5 = {"SC3021234", "08:00:00", "11:25:25"};
//	personInfo tmp6 = {"CS301133", "21:45:00", "21:58:40"};
//
//	vector<personInfo> SingleDayData1;
//	SingleDayData1.push_back(tmp1);
//	inoutData.push_back(SingleDayData1);
//
//	vector<personInfo> SingleDayData2;
//	SingleDayData2.push_back(tmp2);
//	SingleDayData2.push_back(tmp3);
//	inoutData.push_back(SingleDayData2);
//
//	vector<personInfo> SingleDayData3;
//	SingleDayData3.push_back(tmp4);
//	SingleDayData3.push_back(tmp5);
//	SingleDayData3.push_back(tmp6);
//	inoutData.push_back(SingleDayData3);
//}

int IsBig(string strSource, string strTarget)
{
	int hourS = atoi(strSource.substr(0,2).c_str());
	int hourT = atoi(strTarget.substr(0,2).c_str());

	int minuteS = atoi(strSource.substr(3,2).c_str());
	int minuteT = atoi(strTarget.substr(3,2).c_str());

	int secondS = atoi(strSource.substr(6,2).c_str());
	int secondT = atoi(strTarget.substr(6,2).c_str());

	if (hourS > hourT)
	{
		return 1;
	}
	else if (hourS < hourT)
	{
		return -1;
	}
	else
	{
		if (minuteS > minuteT)
		{
			return 1;
		}
		else if (minuteS < minuteT)
		{
			return -1;
		}
		else
		{
			if (secondS > secondT)
			{
				return 1;
			}
			else if (secondS < secondT)
			{
				return -1;
			}
			else
			{
				return 0;
			}
		}
	}
}

string GetOrderByIntime(vector<personInfo> SingleDayData)
{
	int ilen = SingleDayData.size();

	if (ilen == 1)
	{
		return SingleDayData[0].name;
	}

	int icount = 0;
	for(int i=0; i<ilen; i++)
	{
		string intime = SingleDayData[0].inTime;
		if (IsBig(intime, SingleDayData[i].inTime) == 1)
		{
			intime = SingleDayData[i].inTime;
			icount = i;
		}
	}
	return SingleDayData[icount].name;
}

string GetOrderByOuttime(vector<personInfo> SingleDayData)
{
	int ilen = SingleDayData.size();

	if (ilen == 1)
	{
		return SingleDayData[0].name;
	}

	int icount = 0;
	for (int i=0; i<ilen; i++)
	{
		string outtime = SingleDayData[0].outTime;
		if (IsBig(outtime, SingleDayData[i].outTime) == -1)
		{
			outtime = SingleDayData[i].outTime;
			icount = i;
		}
	}
	return SingleDayData[icount].name;
}

void GetOrder(vector<vector<personInfo> > inoutData)
{
	for (vector<vector<personInfo> >::const_iterator iter=inoutData.begin(); iter!=inoutData.end(); iter++)
	{
		cout<<GetOrderByIntime(*iter)<<" "<<GetOrderByOuttime(*iter)<<endl;
	}
}

void InputData()
{
	vector<vector<personInfo> > inoutData;

	int iDays;
	while(cin>>iDays)
	{
		for (int i=0; i<iDays; i++)
		{
			int iSingleDay;
			cin>>iSingleDay;

			vector<personInfo> SingleDayData;
			for (int j=0; j<iSingleDay; j++)
			{
				personInfo tmp;
				cin>>tmp.name>>tmp.inTime>>tmp.outTime;
				SingleDayData.push_back(tmp);
			}
			inoutData.push_back(SingleDayData);
		}

	}
}

int main()
{
	vector<vector<personInfo> > inoutData;
	//fillData(inoutData);

	int iDays;
	while(cin>>iDays)
	{
		for (int i=0; i<iDays; i++)
		{
			int iSingleDay;
			cin>>iSingleDay;

			vector<personInfo> SingleDayData;
			for (int j=0; j<iSingleDay; j++)
			{
				personInfo tmp;
				cin>>tmp.name>>tmp.inTime>>tmp.outTime;
				SingleDayData.push_back(tmp);
			}
			inoutData.push_back(SingleDayData);
		}
		GetOrder(inoutData);
	}
	

	system("pause");
	return 1;
}